package com.miplus.ccrm.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.miplus.ccrm.common.*;
import com.miplus.ccrm.core.controller.vo.action.ICmdReqVo;
import com.miplus.ccrm.core.controller.vo.action.ICmdResVo;
import com.miplus.ccrm.core.controller.vo.ficase.*;
import com.miplus.ccrm.core.controller.vo.iou.*;
import com.miplus.ccrm.core.security.JCommonData;
import com.miplus.ccrm.core.service.collect.FiCreditcasesService;
import com.miplus.ccrm.core.service.iou.FiCasesServiceIou;
import com.miplus.ccrm.core.service.iou.IouBillPaydetailService;
import com.miplus.ccrm.core.service.iou.IouBillPayplanService;
import com.miplus.ccrm.core.service.iou.IouBillService;
import com.miplus.ccrm.core.utils.EnumFiCaseQueue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 2-9.合同类案件服务接口
 */
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/iou")
public class IouController {

    @Resource
    private JCommonData jCommonData;
    @Resource
    private FiCasesServiceIou fiCasesServiceIou;
    @Resource
    private IouBillService billService;
    @Resource
    private IouBillPayplanService billPayplanService;
    @Resource
    private IouBillPaydetailService paydetailService;

    /**
     * 取案件池GRID
     */
    @PostMapping("/caseList")
    public R<PageData<ICaseResVo>> caseList(@RequestBody @Validated ICaseReqVo caseReqVo){
        JSysUser user = jCommonData.getCurrUsr();
        IPage<ICaseResVo>  quyResult=fiCasesServiceIou.caseList(user,caseReqVo);
        if(JPubUtils.IsEqual(caseReqVo.getIsdistquy(),"1")){
            //在待分案查询条件需要保存，为了分案时取
            String key=jCommonData.getDistQuyKey(user.getUsrid());
            //有效期2小时
            boolean b=jCommonData.setData(key,caseReqVo,JPubUtils.distExpiration);
            log.info("caseList.setDistQuyKey({},{}):ret={}",key,caseReqVo,b);
        }
        else if(JPubUtils.IsEqual(caseReqVo.getIsrecyquy(),"1")){
            //在案件池一键回收查询条件需要保存，为了回收时取
            String key=jCommonData.getRecyQuyKey(user.getUsrid());
            //有效期2小时
            boolean b=jCommonData.setData(key,caseReqVo,JPubUtils.distExpiration);
            log.info("caseList.setRecyQuyKey({},{}):ret={}",key,caseReqVo,b);
        }
        else if(JPubUtils.IsEqual(caseReqVo.getIsassquy(),"1")){//2024-08-27 add
            //待派工一键派工操作
            String key=jCommonData.getAssQuyKey(user.getUsrid());
            //有效期2小时
            boolean b=jCommonData.setData(key,caseReqVo,JPubUtils.distExpiration);
            log.info("caseList.setAssQuyKey({},{}):ret={}",key,caseReqVo,b);
        }
        else if(JPubUtils.IsEqual(caseReqVo.getQuynextcase(),"1")) {//2024-10-30
            //取下一条数据条件及数据
            String key=jCommonData.getQuyNextCaseKey(user.getUsrid());
            //有效期2小时
            boolean b=jCommonData.setData(key,caseReqVo,JPubUtils.distExpiration*2);
            log.info("caseList.SetQuyNextCaseKey({},{}):ret={}",key,caseReqVo,b);

            key=jCommonData.getQuyNextCaseDataKey(user.getUsrid());
            b=jCommonData.setData(key,quyResult,JPubUtils.distExpiration*2);
            log.info("caseList.SetQuyNextCaseDataKey({}):ret={}",key,b);
        }
        return R.SuccessPage(quyResult);
    }

    /**
     * 查询结果逾期金额汇总
     * 参数有数组参数，因此使用POST
     */
    @PostMapping("/getQueryDebtSum")
    public R<QuyAmtSumVo> getQueryDebtSum(@RequestBody @Validated ICaseReqVo caseReqVo){
        JSysUser user = jCommonData.getCurrUsr();
        return R.Success(fiCasesServiceIou.getQueryDebtSum(user,caseReqVo));
    }

    /**
     * 取案件详细信息
     */
    @GetMapping("/caseInfo")
    public R<ICaseInfoVo> caseInfo(@RequestParam @NotNull String ficaseid){
        JSysUser user = jCommonData.getCurrUsr();
        return R.Success(fiCasesServiceIou.getCaseDetailInfo(user,Integer.parseInt(ficaseid)));
    }

    /**
     * 取借据信息List
     */
    @GetMapping("/billList")
    public R<PageData<IBillResVo>> billList(@Validated IBillReqVo billReqVo){
        JSysUser user = jCommonData.getCurrUsr();
        return R.SuccessPage(billService.caseList(user,billReqVo));
    }

    /**
     * 取借据还款计划Page
     */
    @GetMapping("/billpayplan")
    public R<PageData<IPayPlanResVo>> billpayplan(@Validated IPayPlanReqVo payPlanReqVo){
        return R.SuccessPage(billPayplanService.payplanList(payPlanReqVo));
    }

    /**
     * 查询借据还款计划List
     */
    @GetMapping("/quybillpayplan")
    public R<List<IPayPlanResVo>> quybillpayplan(@RequestParam @NotNull String billno){
        return R.Success(billPayplanService.quyPayplan(billno));
    }

    /**
     * 取借据还款明细List
     */
    @GetMapping("/billpaydetail")
    public R<PageData<IPayDetailResVo>> billpaydetail(@Validated IPayDetailReqVo payDetailReqVo){
        return R.SuccessPage(paydetailService.caseList(payDetailReqVo));
    }

    /**
     * 分配案件到部门或机构
     */
    @PostMapping("/fiCaseDistribute")
    public R<List<DistResVo>> fiCaseDistribute(@RequestBody @Validated DistReqVo distReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        String $usrid= user.getUsrid();
        ICaseReqVo quyparas=null;
        if(JPubUtils.IsEqual(distReqVo.getIsquydist(),"1")){//2024-06-09 一键分案
            String key=jCommonData.getDistQuyKey($usrid);
            Object o=jCommonData.getData(key);
            if(o==null){
                log.warn("fiCaseDistribute:nofind {}",key);
                return R.Error(EnumRCode.NoFindQuyPara);
            }else{
                quyparas=(ICaseReqVo)o;
                if(!JPubUtils.IsEqual(quyparas.getCcc(), EnumFiCaseQueue.O.name)){
                    //2024-09-27 异常检查，为了不使用全部数据
                    log.warn("fiCaseDistribute:ccc Error={}",quyparas.getCcc());
                    return R.Error(EnumRCode.ArgErr);
                }
            }
        }
        return R.Success(fiCasesServiceIou.caseDistribute(user,distReqVo,quyparas));
    }

    /**
     * 分配案件到催员(派工)
     */
    @PostMapping("/fiCaseAssgin")
    public R<List<DistResVo>> fiCaseAssgin(@RequestBody @Validated AssReqVo assReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        String $usrid= user.getUsrid();
        ICaseReqVo quyparas=null;
        if(JPubUtils.IsEqual(assReqVo.getIsquydist(),"1")){//2024-06-09 一键分案
            String key="";
            if(JPubUtils.IsNotEmpty(assReqVo.getGrpid())){//直接派工
                key=jCommonData.getDistQuyKey($usrid);
            }else {
                key=jCommonData.getAssQuyKey($usrid);
            }
            Object o=jCommonData.getData(key);
            if(o==null){
                log.warn("fiCaseAssgin:nofind {}",key);
                return R.Error(EnumRCode.NoFindQuyPara);
            }else{
                quyparas=(ICaseReqVo)o;
                if(!JPubUtils.IsEqual(quyparas.getCcc(), EnumFiCaseQueue.Q.name)){
                    //2024-09-27 异常检查，为了不使用全部数据
                    log.warn("fiCaseAssgin:ccc Error={}",quyparas.getCcc());
                    return R.Error(EnumRCode.ArgErr);
                }
            }
        }
        return R.Success(fiCasesServiceIou.caseAssign(user,assReqVo,quyparas));
    }

    /**
     * 回收案件
     */
    @PostMapping("/fiCaseRecycle")
    public R<CmdResVo> fiCaseRecycle(@RequestBody @Validated RecyReqVo recyReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        String $usrid= user.getUsrid();
        ICaseReqVo quyparas=null;
        if(JPubUtils.IsEqual(recyReqVo.getIsquyrecy(),"1")){//2024-07-28 一键回收
            String key=jCommonData.getRecyQuyKey($usrid);
            Object o=jCommonData.getData(key);
            if(o==null){
                log.warn("fiCaseRecycle:nofind {}",key);
                return R.Error(EnumRCode.NoFindQuyPara);
            }else{
                quyparas=(ICaseReqVo)o;
            }
        }
        return R.Success(fiCasesServiceIou.caseRecycle(user,recyReqVo,quyparas));
    }

    /**
     * 转派案件到委外机构
     */
    @PostMapping("/fiCaseTranDist")
    public R<CmdResVo> fiCaseTranDist(@RequestBody @Validated TranDistReqVo tranDistReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        String $usrid= user.getUsrid();
        ICaseReqVo quyparas=null;
        if(JPubUtils.IsEqual(tranDistReqVo.getIsquydist(),"1")){
            String key=jCommonData.getDistQuyKey($usrid);
            Object o=jCommonData.getData(key);
            if(o==null){
                log.warn("fiCaseTranDist:nofind {}",key);
                return R.Error(EnumRCode.NoFindQuyPara);
            }else{
                quyparas=(ICaseReqVo)o;
                if(!JPubUtils.IsEqual(quyparas.getCcc(), EnumFiCaseQueue.O.name)){
                    //2024-09-27 异常检查，为了不使用全部数据
                    log.warn("fiCaseTranDist:ccc Error={}",quyparas.getCcc());
                    return R.Error(EnumRCode.ArgErr);
                }
            }
        }
        return R.Success(fiCasesServiceIou.caseTranDist(user,tranDistReqVo,quyparas));
    }

    /**
     * 案件导出
     */
    @PostMapping("/expFiCase")
    public R<CmdResVo> expFiCase(@RequestBody @Validated ICaseReqVo iCaseReqVo, HttpServletResponse response){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(fiCasesServiceIou.caseSaveAs(user,iCaseReqVo,response));
    }

    /**
     * 取一键回收案件数
     */
    @GetMapping("/getOneRecyCount")
    public R<CmdResVo> getOneRecyCount(){
        JSysUser user= jCommonData.getCurrUsr();
        String key=jCommonData.getRecyQuyKey(user.getUsrid());
        Object o=jCommonData.getData(key);
        ICaseReqVo quyparas=null;
        if(o==null){
            log.warn("getOneRecyCount:nofind {}",key);
            return R.Error(EnumRCode.NoFindQuyPara);
        }else{
            quyparas=(ICaseReqVo)o;
        }
        return R.Success(fiCasesServiceIou.getOneRecyCount(user,quyparas));
    }

    /**
     * 案件详细取下一案件ID
     */
    @GetMapping("/getNextFiCaseID")
    public R<CmdResVo> getNextFiCaseID(@RequestParam @NotNull Integer ficaseid){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(fiCasesServiceIou.getNextFiCaseID(user,ficaseid));
    }

    /**
     * 刷新最新欠款
     */
    @PostMapping("/refreshPayBal")
    public R<ICmdResVo> refreshPayBal(@RequestBody @Validated ICmdReqVo reqVo){
        return R.Success(billService.refreshPayBal(reqVo));
    }
}
